/**
 * Title UserManagerServiceImpl
 * Package com.sba.manage.service
 * Copyright 2024 www.hundsun.com All Rights Reserved.
 *
 * @author gd
 * @date 2024/4/28 3:28 PM
 * @version V5.1.2
 */
package com.ruoyi.manage.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.Seq;
import com.ruoyi.manage.db.dao.SbaUsersMapper;
import com.ruoyi.manage.db.model.SbaUsers;
import com.ruoyi.manage.db.model.SbaUsersExample;
import com.ruoyi.manage.service.IUserManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 用户管理服务
 * ClassName UserManagerServiceImpl
 *
 * @author gd
 * @date 2024/4/28 3:28 PM
 */
@Service
public class UserManagerServiceImpl implements IUserManager {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SbaUsersMapper sbaUsersMapper;


    /**
     * 用户注册
     *
     * @param user 待注册个人用户信息
     * @return 个人用户信息
     */
    @Override
    public SbaUsers register(SbaUsers user) {
        //根据手机号进行唯一性检测
        String userId = Seq.getId();
        user.setUserId(userId);
        user.setRegistrationDate(new Date());
        user.setLastLoginDate(new Date());
        long i = sbaUsersMapper.insert(user);
        if (i <= 0) {
            logger.error("用户注册异常");
            return null;
        }
        return user;
    }

    /**
     * 用户数量统计
     *
     * @param user 用户查询条件
     * @return 统计数量
     */
    @Override
    public long count(SbaUsers user) {
        //根据手机号进行唯一性检测
        SbaUsersExample example = new SbaUsersExample();
        SbaUsersExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(user.getPhone())) {
            criteria.andPhoneEqualTo(user.getPhone());
        }
        if (StringUtils.isNotBlank(user.getAppleOpenId())) {
            criteria.andAppleOpenIdEqualTo(user.getAppleOpenId());
        }
        if (StringUtils.isNotBlank(user.getEmail())) {
            criteria.andEmailEqualTo(user.getEmail());
        }
        if (StringUtils.isNotBlank(user.getWeChatOpenId())) {
            criteria.andWeChatOpenIdEqualTo(user.getWeChatOpenId());
        }
        return sbaUsersMapper.countByExample(example);
    }

    /**
     * 根据用户信息查询用户
     *
     * @param user 用户查询条件
     * @return 用户信息
     */
    @Override
    public SbaUsers queryByUsers(SbaUsers user) {
        SbaUsersExample example = new SbaUsersExample();
        SbaUsersExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(user.getPhone())) {
            criteria.andPhoneEqualTo(user.getPhone());
        }
        if (StringUtils.isNotBlank(user.getAppleOpenId())) {
            criteria.andAppleOpenIdEqualTo(user.getAppleOpenId());
        }
        if (StringUtils.isNotBlank(user.getEmail())) {
            criteria.andEmailEqualTo(user.getEmail());
        }
        if (StringUtils.isNotBlank(user.getWeChatOpenId())) {
            criteria.andWeChatOpenIdEqualTo(user.getWeChatOpenId());
        }
        List<SbaUsers> ls = sbaUsersMapper.selectByExample(example);
        if (null != ls && ls.size() > 0) {
            return ls.get(0);
        }
        return null;
    }

    /**
     * 更新用户信息
     *
     * @param user 用户更新条件
     * @return 更新结果
     */
    @Override
    public int updateByUsers(SbaUsers queryUser, SbaUsers user) {
        SbaUsersExample example = new SbaUsersExample();
        SbaUsersExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(queryUser.getPhone())) {
            criteria.andPhoneEqualTo(queryUser.getPhone());
        }
        if (StringUtils.isNotBlank(queryUser.getAppleOpenId())) {
            criteria.andAppleOpenIdEqualTo(queryUser.getAppleOpenId());
        }
        if (StringUtils.isNotBlank(queryUser.getEmail())) {
            criteria.andEmailEqualTo(queryUser.getEmail());
        }
        if (StringUtils.isNotBlank(queryUser.getWeChatOpenId())) {
            criteria.andWeChatOpenIdEqualTo(queryUser.getWeChatOpenId());
        }
        return sbaUsersMapper.updateByExample(user, example);
    }

    /**
     * 验证用户 token
     *
     * @param token token
     * @param user  用户登录信息
     * @return 成功与否
     */
    @Override
    public boolean checkUserToken(String token, SbaUsers user) {
        SbaUsersExample example = new SbaUsersExample();
        SbaUsersExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(user.getPhone())) {
            criteria.andPhoneEqualTo(user.getPhone());
        }
        if (StringUtils.isNotBlank(user.getAppleOpenId())) {
            criteria.andAppleOpenIdEqualTo(user.getAppleOpenId());
        }
        if (StringUtils.isNotBlank(user.getEmail())) {
            criteria.andEmailEqualTo(user.getEmail());
        }
        if (StringUtils.isNotBlank(user.getWeChatOpenId())) {
            criteria.andWeChatOpenIdEqualTo(user.getWeChatOpenId());
        }
        long i = sbaUsersMapper.countByExample(example);
        if (i <= 0) {
            return false;
        }
        return true;
    }
}